package com.skcc.cop.mrtaxi.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;

public class StringUtil {

	public static String intoString(InputStream inputStream) {
		BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
		StringBuilder sb = new StringBuilder();
		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				inputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}

	public static String[] intoStringArray(ArrayList<String> arrayList) {
		if (arrayList.size() != 0) {
			Object[] objectArray = arrayList.toArray();
			return Arrays.asList(objectArray).toArray(new String[objectArray.length]);
		}
		return null;
	}

	public static String intoMD5(String value) {
		StringBuffer md5 = new StringBuffer();
		try {
			byte[] digest = java.security.MessageDigest.getInstance("MD5").digest(value.getBytes());
			for (int i = 0; i < digest.length; i++) {
				md5.append(Integer.toString((digest[i] & 0xf0) >> 4, 16));
				md5.append(Integer.toString(digest[i] & 0x0f, 16));
			}

		} catch (java.security.NoSuchAlgorithmException ne) {
			ne.printStackTrace();
		}
		return md5.toString();
	}

	public static ArrayList<String> getSortList(ArrayList<String> beforeList) {
		HashSet<String> hashSet = new HashSet<String>(beforeList);
		ArrayList<String> afterList = new ArrayList<String>(hashSet);
		Collections.sort(afterList);
		return afterList;
	}
	
	public static ArrayList<String> getSplitArray(String str, String separator){
		if(str != null){
			ArrayList<String> reList = new ArrayList<String>();
			String temp[] = str.split(separator);
			if(temp.length > 0 && !(temp.length == 1 && temp[0].length() == 0)){
				for(int i=0;i<temp.length;i++){
					reList.add(temp[i]);
				}	
			}
			return reList;
		}
		return null;
	}
}
